# 查找头文件
find_path(JsonCpp_INCLUDE_DIR
    NAMES json/json.h
    PATHS
        /usr/include
        /usr/local/include
        /opt/local/include
        ${CMAKE_PREFIX_PATH}/include
    PATH_SUFFIXES jsoncpp  # 有些系统会把头文件放在 jsoncpp/json/json.h 中
)

# 查找库文件
find_library(JsonCpp_LIBRARY
    NAMES jsoncpp libjsoncpp jsoncpp_lib
    PATHS
        /usr/lib
        /usr/local/lib
        /opt/local/lib
        ${CMAKE_PREFIX_PATH}/lib
)

# 设置输出变量
if(JsonCpp_INCLUDE_DIR AND JsonCpp_LIBRARY)
    set(JsonCpp_FOUND TRUE)
    set(JsonCpp_INCLUDE_DIRS ${JsonCpp_INCLUDE_DIR})
    set(JsonCpp_LIBRARIES ${JsonCpp_LIBRARY})
    # 创建导入目标（可选，但推荐） 
    if(NOT TARGET JsonCpp::JsonCpp)
        add_library(JsonCpp::JsonCpp UNKNOWN IMPORTED)
        set_target_properties(JsonCpp::JsonCpp PROPERTIES
        IMPORTED_LOCATION "${JsonCpp_LIBRARY}"
        INTERFACE_INCLUDE_DIRECTORIES "${JsonCpp_INCLUDE_DIRS}"
    )
    endif()
else()
    set(JsonCpp_FOUND FALSE)
endif()

# 标记为高级变量（在 ccmake 或 cmake-gui 中隐藏） 
mark_as_advanced(JsonCpp_INCLUDE_DIR JsonCpp_LIBRARY)
